﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Dreaming_of_Space_2
{
    public partial class MainForm : Form
    {
 
        public int x_MouseDown_1, y_MouseDown_1;
        //защита солнечной системы
        SolarPRO OurPRO = new SolarPRO();
        //планета Земля
        Planet Earth = new Planet();

        //формирование нашествия
        SpaceInvasion InvasionWave = new SpaceInvasion();
 
        public MainForm()
        {
            InitializeComponent();
           
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            timer1.Enabled = true;
            timer1.Interval = 50;

            //Используем двойную буферизацию:
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint, true);

            InvasionWave.UfoInvasion = new SpaceShip[InvasionWave.MaxUfoQuantity];
            for (int i = 0; i < InvasionWave.MaxUfoQuantity; i++)
                InvasionWave.UfoInvasion[i] = new SpaceShip(); 
           
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox1.AppendText(OurPRO.money.ToString());
            Refresh();
            
         
        }
         
        private void button1_Click(object sender, EventArgs e)
        {
            if ((InvasionWave.CheckWaveIsDestroyed() == false)&&(InvasionWave.current_number_invasion_wave>0)) return;
                InvasionWave.current_number_invasion_wave++;
                OurPRO.money += 5000;
                timer2.Enabled = true;
                timer2.Interval = 2000;
                InvasionWave.CreateInvasionWave();
                InvasionWave.start_invasion = true;
                
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //Определяем координаты указателя в момент нажатия кнопки:
            x_MouseDown_1 = e.X; y_MouseDown_1 = e.Y;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {   
            OurPRO.DetectSelectObject(x_MouseDown_1, y_MouseDown_1);
            if (OurPRO.CheckDistanse(x_MouseDown_1, y_MouseDown_1) == false) return;
            if (OurPRO.set_tower_enabled == false) return;
            if ((OurPRO.DetectSelectObject(x_MouseDown_1, y_MouseDown_1) == false) && (OurPRO.money >= 2000))
            {
                Graphics myGraphics = CreateGraphics();
                OurPRO.SetNewTower(x_MouseDown_1, y_MouseDown_1, myGraphics);
                Cursor = Cursors.Arrow;
            }
        }

        public bool OpenFire(PaintEventArgs e)
        {
            Pen myPen = new Pen(Color.Red, 1);
            
                for (int j = 0; j < InvasionWave.CurrentInvasionQuantity; j++)
                    for (int i = 0; i < OurPRO.TowerQuantity; i++)
               {
                   
                   if ((InvasionWave.UfoInvasion[j].pos_of_ufo_x - OurPRO.ProSolarSys[i].Pos.X) *
                       (InvasionWave.UfoInvasion[j].pos_of_ufo_x - OurPRO.ProSolarSys[i].Pos.X) +
                       (InvasionWave.UfoInvasion[j].pos_of_ufo_y - OurPRO.ProSolarSys[i].Pos.Y) *
                       (InvasionWave.UfoInvasion[j].pos_of_ufo_y - OurPRO.ProSolarSys[i].Pos.Y)
                       < OurPRO.ProSolarSys[i].fire_distance * OurPRO.ProSolarSys[i].fire_distance)
                   {
                       if (InvasionWave.UfoInvasion[j].armor <= 0)
                       {
                           OurPRO.money += InvasionWave.UfoInvasion[j].cargo;
                           InvasionWave.UfoInvasion[j].cargo = 0;
                           continue;
                       }


                      
                       e.Graphics.DrawLine(myPen, OurPRO.ProSolarSys[i].Pos.X, OurPRO.ProSolarSys[i].Pos.Y,
                           InvasionWave.UfoInvasion[j].pos_of_ufo_x, InvasionWave.UfoInvasion[j].pos_of_ufo_y);
                       //нанесение повреждений
                       InvasionWave.UfoInvasion[j].armor = InvasionWave.UfoInvasion[j].armor - OurPRO.ProSolarSys[i].damage;

                   }
               }
                
           
             return true;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Bitmap mySpace = new Bitmap( Application.StartupPath+"\\solar_system_bg.bmp");
            e.Graphics.DrawImage(mySpace, 0, 0);
            InvasionWave.DrawInvasion(e, Earth);
            //перепрорисовка башен
            for (int i = 0; i < OurPRO.TowerQuantity; i++)
                OurPRO.ProSolarSys[i].SetTower(OurPRO.ProSolarSys[i].Pos.X, OurPRO.ProSolarSys[i].Pos.Y, e.Graphics);
            //прорисовка жизни планеты
            Earth.Draw_Planet_life(e);
            if (Earth.planet_life <= 0)
            {
                timer1.Stop();
                timer2.Stop();
                MessageBox.Show("Сражение проиграно", "Но решающие сражения впереди!",
                                      MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
                //Application.Exit();

            }
            //ведение огня
            OpenFire(e);
           
         }

        private void button2_Click(object sender, EventArgs e)
        {
            if (OurPRO.TowerQuantity < OurPRO.MaxTowerQuantity) 
            {
                OurPRO.set_tower_enabled = true;
                Cursor = Cursors.Cross;
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < OurPRO.TowerQuantity; i++)
                if ((OurPRO.ProSolarSys[i].select == true) && 
                    (OurPRO.ProSolarSys[i].fire_distance < OurPRO.max_fire_distance) && 
                    (OurPRO.money >= 300))
                {
                    OurPRO.ProSolarSys[i].fire_distance += 10;
                    OurPRO.money -= 300;
                    
                }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < OurPRO.TowerQuantity; i++)
                if ((OurPRO.ProSolarSys[i].select == true) 
                    && (OurPRO.ProSolarSys[i].damage < OurPRO.max_damage)
                    && (OurPRO.money >= 300))
                {
                    OurPRO.ProSolarSys[i].damage += 1;
                    OurPRO.money -= 300;

                }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (InvasionWave.UfoQuantity < InvasionWave.CurrentInvasionQuantity)
                InvasionWave.UfoQuantity++;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (OurPRO.TowerQuantity < OurPRO.MaxTowerQuantity)
            {
                if (OurPRO.set_tower_enabled == true)
                {
                    OurPRO.set_tower_enabled = false;
                    Cursor = Cursors.Arrow;
                }
                else
                {
                    OurPRO.set_tower_enabled = true;
                    Cursor = Cursors.Cross;
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        

       

    }

 
}
